package cn.stylefeng.guns.modular.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.auth.model.LoginUser;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.dto.FuzzySearchSframeDto;
import cn.stylefeng.guns.modular.entity.ScheduleForm;
import cn.stylefeng.guns.modular.entity.ScheduleFrame;
import cn.stylefeng.guns.modular.service.ScheduleFormService;
import cn.stylefeng.guns.modular.service.ScheduleFrameService;
import cn.stylefeng.guns.modular.utils.UserMessage;
import cn.stylefeng.guns.modular.wapper.MyScheduleFrameWrapper;
import cn.stylefeng.guns.modular.wapper.MyscheduleformWrapper;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/myScheduleDraft")
public class MyScheduleDraftController {
    @Autowired
    private ScheduleFormService scheduleFormService;

    @Autowired
    private ScheduleFrameService scheduleFrameService;

    /**
     * 加载草稿箱首页
     * @return
     */
    @RequestMapping("/showMyScheduleDraft")
    public String loadMyScheduleDraft(){
        return "/scheduledraft/showMyScheduleDraft.html";
    }
    /**
     * 显示草稿箱首页
     * @return
     */
    @RequestMapping(value = "/showMyScheduleDraft",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject showMyScheduleDraft(){

         //得到当前登陆者的信息
        long userId= UserMessage.getUserId();

        List<ScheduleForm> draftScheduleForms= scheduleFormService.showAllDraftSform(userId);
        System.out.println("==============scheduleForms==========="+draftScheduleForms);
        List<ScheduleFrame> draftScheduleFrames =scheduleFrameService.showAllSframeDraft(userId);
        System.out.println("===========scheduleFrames=============="+draftScheduleFrames);
        Map<String,Object> map =new HashMap();
        map.put("draftScheduleForms",draftScheduleForms);
        map.put("draftScheduleFrames",draftScheduleFrames);

        JSONObject result = JSONObject.parseObject(JSON.toJSONString(map));
        System.out.println("===============result================="+result);
        return result;
    }

    /**
     * 加载到草稿日程首页
     * @return
     */
    @RequestMapping("/loadSformDraft")
    public String loadSformDraft(){
        return "/scheduledraft/showMyScheduleFormDraft.html";
    }


    /**
     * 显示所有草稿日程
     */
    @RequestMapping(value = "/showSformDraft" ,method = RequestMethod.POST)
    @ResponseBody
    public Object ShowDraftSform(String keyword,String creatTime){

        System.out.println("=========1234=keyword======="+keyword+creatTime);

        if (keyword!=null&keyword!=""){
            keyword=keyword.trim();
        }

        Page<Map<String, Object>> DraftSforms =scheduleFormService.fuzzyShowDraftSform(keyword,creatTime);

        Page wrapped = new MyscheduleformWrapper(DraftSforms).wrap();
        LayuiPageInfo layuiPageInfo =LayuiPageFactory.createPageInfo(wrapped);
        System.out.println("================================"+layuiPageInfo);
        return layuiPageInfo;

    }

    /**
     * 加载到草稿日程框架首页
     * @return
     */
    @RequestMapping("/loadSframeDraft")
    public String loadSframeDraft(){
        return "/scheduledraft/showMyScheduleFrameDraft.html";
    }


    /**
     * 显示所有草稿日程框架
     */
    @RequestMapping(value = "/showSframeDraft" ,method = RequestMethod.POST)
    @ResponseBody
    public Object ShowDraftSframe(String keyword,String creatTime){
        System.out.println("=========1234=keyword======="+keyword+creatTime);
        if (keyword!=null&keyword!=""){
            keyword=keyword.trim();
        }
        Page<Map<String, Object>> DraftSframes =scheduleFrameService.fuzzyShowDraftSframe(keyword,creatTime);

        Page wrapped = new MyScheduleFrameWrapper(DraftSframes).wrap();
        LayuiPageInfo layuiPageInfo =LayuiPageFactory.createPageInfo(wrapped);
        System.out.println("============layuiPageInfo===================="+layuiPageInfo);
        return layuiPageInfo;

    }

}
